package com.wj.hourupdate.datasource;

import java.util.ArrayList;
import java.util.List;

import com.wj.hourupdate.datasource.NewsData.News;
import com.wj.hourupdate.util.Logger;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class NewsDatasource extends SQLiteDatasource{
	private String[] allColumn = {
			DatabaseHelper.TB_NEWS_COL_CNID,
			DatabaseHelper.TB_NEWS_COL_CNLINK,
			DatabaseHelper.TB_NEWS_COL_CNLOGO,
			DatabaseHelper.TB_NEWS_COL_CDNID,
			DatabaseHelper.TB_NEWS_COL_CDNTITLE,
			DatabaseHelper.TB_NEWS_COL_CDNDESC,
			DatabaseHelper.TB_NEWS_COL_CDNLINK,
			DatabaseHelper.TB_NEWS_COL_CDNPUBDATE,
			DatabaseHelper.TB_NEWS_COL_CDNIMAGE,
			DatabaseHelper.TB_NEWS_COL_BANNER,		
	};
	
	public NewsDatasource(Context c) {
		super(c);
	}

	public NewsData.News listNews(){
		NewsData.News news = new NewsData.News();
		Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TB_NEWS +
				" ORDER BY " + DatabaseHelper.TB_NEWS_COL_CDNID + " DESC LIMIT 1", null);
		
		if(cursor.moveToFirst()){
			news = 
					new NewsData.News(
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNID)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNLINK)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNLOGO)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNID)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNTITLE)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNDESC)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNLINK)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNPUBDATE)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNIMAGE)),
							cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_BANNER)));
		}
		cursor.close();
		return news;
	}
	
	public List<NewsData.News> listAllNews(){
		List<NewsData.News> newsLst = new ArrayList<NewsData.News>();
		Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TB_NEWS +
				" ORDER BY " + DatabaseHelper.TB_NEWS_COL_CDNID +
				" LIMIT 20 ", null);
		if(cursor.moveToFirst()){
			do{
				NewsData.News news = 
						new NewsData.News(
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNID)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNLINK)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CNLOGO)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNID)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNTITLE)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNDESC)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNLINK)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNPUBDATE)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_CDNIMAGE)),
								cursor.getString(cursor.getColumnIndex(DatabaseHelper.TB_NEWS_COL_BANNER)));
				newsLst.add(news);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return newsLst;
	}
	
	public void insertNews(NewsData newsData){
		for(NewsData.News news : newsData.getNews()){
			ContentValues cv = new ContentValues();
			cv.put(DatabaseHelper.TB_NEWS_COL_CNID, news.getCnid());
			cv.put(DatabaseHelper.TB_NEWS_COL_CNLINK, news.getCnlink());
			cv.put(DatabaseHelper.TB_NEWS_COL_CNLOGO, news.getCnlogo());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNID, news.getCdnid());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNTITLE, news.getCdntitle());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNDESC, news.getCdndesc());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNLINK, news.getCdnlink());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNPUBDATE, news.getCdnpubdate());
			cv.put(DatabaseHelper.TB_NEWS_COL_CDNIMAGE, news.getCdnimage());
			cv.put(DatabaseHelper.TB_NEWS_COL_BANNER, news.getBanner());
			
			try {
				db.insert(DatabaseHelper.TB_NEWS, null, cv);
			} catch (Exception e) {
				log.appendLog(e.getMessage());
			}
		}
	}
}
